//
//  XLEditIndexAdapter.m
//  industry
//
//  Created by xl on 2020/11/27.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLEditIndexAdapter.h"
#import "XLEditIndexCell.h"
#import "XLEditIndexVC.h"

@interface XLEditIndexAdapter ()

@property (nonatomic, strong) UITableView *tableView;

@end
@implementation XLEditIndexAdapter

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    self.tableView = tableView;
    return self.getAdapterArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 56.0f;;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section > self.getAdapterArray.count -1) {
        return [XLTableViewCell new];
    }
    static NSString *cellIdentifierID = @"XLAllIndexCell";
    XLEditIndexCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierID];
    if (!cell) {
        cell = [[XLEditIndexCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierID];
    }
    [cell loadCellData:self.getAdapterArray[indexPath.section]];
    
    //添加长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognized:)];
    [cell addGestureRecognizer:longPress];
    
    return cell;
}

#pragma mark - UILongPressGestureRecognizer
- (void)longPressGestureRecognized:(id)sender {
    XLEditIndexVC *vc = (XLEditIndexVC *)self.tableView.baseController;
    vc.isChange = YES;
    
    UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
    UIGestureRecognizerState state = longPress.state;
    
    CGPoint location = [longPress locationInView:_tableView];
    NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:location];
    static UIView       *snapshot = nil;
    static NSIndexPath  *sourceIndexPath = nil;
    switch (state) {
        case UIGestureRecognizerStateBegan: {
            if (indexPath) {
                sourceIndexPath = indexPath;
                UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
                // Take a snapshot of the selected row using helper method.
                snapshot = [self customSnapshoFromView:cell];
                // Add the snapshot as subview, centered at cell's center...
                __block CGPoint center = cell.center;
                snapshot.center = center;
                snapshot.alpha = 0.0;
                [_tableView addSubview:snapshot];
                [UIView animateWithDuration:0.25 animations:^{
                    // Offset for gesture location.
                    center.y = location.y;
                    snapshot.center = center;
                    snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);
                    snapshot.alpha = 0.98;
                    
                    cell.alpha = 0.0f;
                } completion:^(BOOL finished) {
                    cell.hidden = YES;
                }];
            }
            break;
        }
            
        case UIGestureRecognizerStateChanged: {
            CGPoint center = snapshot.center;
            center.y = location.y;
            snapshot.center = center;
            
            // Is destination valid and is it different from source?
            if (indexPath && ![indexPath isEqual:sourceIndexPath]) {
                NSMutableArray *temArr = [NSMutableArray arrayWithArray:self.getAdapterArray];
                [temArr exchangeObjectAtIndex:indexPath.section withObjectAtIndex:sourceIndexPath.section];
                [self setAdapterArray:temArr];
                vc.indexArr = temArr;
                [_tableView moveSection:sourceIndexPath.section toSection:indexPath.section];
                sourceIndexPath = indexPath;
            }
            break;
        }
            
        default: {
            // Clean up.
            UITableViewCell *cell = [_tableView cellForRowAtIndexPath:sourceIndexPath];
            [UIView animateWithDuration:0.25 animations:^{
                
                snapshot.center = cell.center;
                snapshot.transform = CGAffineTransformIdentity;
                snapshot.alpha = 0.0;
                
                cell.alpha = 1.0f;
            } completion:^(BOOL finished) {
                cell.hidden = NO;
                [snapshot removeFromSuperview];
                snapshot = nil;
                
            }];
            sourceIndexPath = nil;
            break;
        }
    }
}

#pragma mark - Private
 - (UIView *)customSnapShortFromViewEx:(UIView *)inputView {
     CGSize inSize = inputView.bounds.size;
     // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了
     UIGraphicsBeginImageContextWithOptions(inSize, NO, [UIScreen mainScreen].scale);
     [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
     UIImage *image= UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     UIImageView* snapshot = [[UIImageView alloc] initWithImage:image];
     return snapshot;
 }

- (UIView *)customSnapshoFromView:(UIView *)inputView {
    UIView* snapshot = nil;
    if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 7.0) {
        //ios7.0 以下通过截图形式保存快照
        snapshot = [self customSnapShortFromViewEx:inputView];
     }else{
         //ios7.0 系统的快照方法
         snapshot = [inputView snapshotViewAfterScreenUpdates:YES];
    }

    snapshot.layer.masksToBounds = NO;
    snapshot.layer.cornerRadius = 0.0;
    snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);
    snapshot.layer.shadowRadius = 5.0;
    snapshot.layer.shadowOpacity = 0.4;
    return snapshot;
 }
@end
